const {
	userCollection
} = require('../../common/constants');
const {
	ERROR
} = require('../../common/error');

module.exports = async function(params = {}) {
	const db = uniCloud.database();

	const {
		uid
	} = this.authInfo;

	const {
		overseerId
	} = params;

	const getUserRes = await userCollection.doc(uid).get();
	const userRecord = getUserRes && getUserRes.data && getUserRes.data[0];

	if (!userRecord) {
		throw {
			errCode: ERROR.ACCOUNT_NOT_EXISTS
		}
	}

	if (!userRecord.mobile) {
		throw {
			errCode: ERROR.UNBIND_MOBILE_NOT_EXISTS,
		}
	};

	if (userRecord.realNameAuth && userRecord.realNameAuth.errCode == 0) {
		throw {
			errCode: ERROR.INVALID_REAL_NAME,
		}
	};

	if (userRecord.member && userRecord.member.status == 0) {
		return {
			errCode: 0,
			data: []
		}
	} else {
		throw {
			errCode: ERROR.ACCOUNT_MEMBER_NOT_ACTIVATED,
		}
	}

	const getOverseerRef = await userCollection.doc(overseerId).get();
	const overseerRecord = getOverseerRef && getOverseerRef.data && getOverseerRef.data[0];

	if (!overseerRecord) {
		throw {
			errCode: ERROR.ACCOUNT_NOT_EXISTS
		}
	}

	return {
		errCode: 0,
		mobile: overseerRecord.mobile
	}
}